package acom.test.swing.aa;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;


public class PrizeJFrame extends Observable{
	

	private List<Prize> prizelist ;
	
	private Prize prize ;
	
	public JFrame frame;
	
	//奖项名称
	JLabel name_lb = null;
	//奖项个数
	JLabel total_lb = null;
	//奖品内容
	JLabel content_lb = null;
	
	JTextField name_tf=null;
	JTextField total_tf=null;
	JTextField content_tf=null;
	
	JButton save_but = null;
	JButton cancel_but =null;
	
	JButton out_but =null;
	
	static final int WIDTH=350;
    static final int HEIGHT=360;
    
	public PrizeJFrame(){
		this.prizelist=SystemUtil.getPrizelist();
		
		frame = new JFrame();
		
		frame.setSize(WIDTH,HEIGHT);
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle("设置奖项");// 设置框架标题
		try {
			// 将LookAndFeel设置成Windows样式
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		
		JPanel contentPane = new JPanel();
		
		
		name_lb = new JLabel("奖项名称", JLabel.CENTER);
		total_lb = new JLabel("奖项数量", JLabel.CENTER);
		content_lb = new JLabel("奖品名称", JLabel.CENTER);
		
		name_tf = new JTextField();
		total_tf = new JTextField();
		content_tf = new JTextField();
				
		save_but = new JButton("保存");
		cancel_but = new JButton("清空");
		out_but = new JButton("退出");
		
		//设置位置、大小
		name_lb.setBounds(32, 20, 80, 40);// 设置标签x坐标32,y坐标12,宽60,长20
		name_tf.setBounds(150, 20, 80, 30);
		
		total_lb.setBounds(32, 80, 80, 40);
		total_tf.setBounds(150, 80, 80, 30);
		
		content_lb.setBounds(32, 140, 80, 40);
		content_tf.setBounds(150, 140, 80, 30);
		
		save_but.setBounds(30,200, 70, 25);
		cancel_but.setBounds(120,200, 70, 25);
		out_but.setBounds(210,200, 70, 25);
		
		
		//设置Action
		save_but.addActionListener(new ActionListener()// 匿名类实现ActionListener接口
		{
			public void actionPerformed(ActionEvent e) {
				save_Action(e);
			}
		});
		cancel_but.addActionListener(new ActionListener()// 匿名类实现ActionListener接口
		{
			public void actionPerformed(ActionEvent e) {
				cancel_Action(e);
			}
		});
		out_but.addActionListener(new ActionListener()// 匿名类实现ActionListener接口
		{
			public void actionPerformed(ActionEvent e) {
				frame.setVisible(false);
			}
		});
				
		contentPane.add(name_lb);
		contentPane.add(total_lb);
		contentPane.add(content_lb);
		contentPane.add(name_tf);
		contentPane.add(total_tf);
		contentPane.add(content_tf);
		
		contentPane.add(save_but);
		contentPane.add(cancel_but);
		contentPane.add(out_but);
		
		contentPane.setLayout(null);
		frame.add(contentPane);
		
		SystemUtil.showInCenter(frame); //在屏幕中间显示
		
		
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				//清除显示的
				name_tf.setText("");
				total_tf.setText("");
				content_tf.setText("");
			}
		});
	}
	
	public void show(){
		frame.setVisible(true);
	}
	
	protected void cancel_Action(ActionEvent e) {		
		//this.setVisible(false);
		//清除显示的
		name_tf.setText("");
		total_tf.setText("");
		content_tf.setText("");
	}


	protected void save_Action(ActionEvent e) {
		String pname = name_tf.getText();
		String ptotal = total_tf.getText();
		String pcontent = content_tf.getText();
		
		if(pname==null||pname.trim().isEmpty()){
			JOptionPane.showMessageDialog(null,"奖项名称不能为空");
			return;
		}
		if(pcontent==null||pcontent.trim().isEmpty()){
			JOptionPane.showMessageDialog(null,"奖品名称不能为空");
			return;
		}
		if(ptotal==null||ptotal.trim().isEmpty()){
			JOptionPane.showMessageDialog(null,"奖项数量不能为空");
			return;
		}
		if(!SystemUtil.isInteger(ptotal)){
			JOptionPane.showMessageDialog(null,"奖项数量不是数字格式");
			return;
		}
		
		//保存 数据
		System.out.println("pname="+pname+", ptotal="+ptotal+", pcontent="+pcontent);
		prize = new Prize();
		prize.setPname(pname);
		prize.setPcontent(pcontent);
		prize.setTotal(Integer.parseInt(ptotal));
		prize.setIsFinish(false);
		prizelist.add(prize);
		
		//清除显示的
		name_tf.setText("");
		total_tf.setText("");
		content_tf.setText("");
		
		//通知观察者
		notifyTheObservers();
	}

	private void notifyTheObservers(){
		this.setChanged();
	    this.notifyObservers();
	}

	public static void main(String[] args) {		
		PrizeJFrame prizeJFrame = new PrizeJFrame();
		prizeJFrame.show();
	}


	public List<Prize> getPrizelist() {
		return prizelist;
	}
	public void setPrizelist(List<Prize> prizelist) {
		this.prizelist = prizelist;
	}
	public Prize getPrize() {
		return prize;
	}

	public void setPrize(Prize prize) {
		this.prize = prize;
	}

}
